//
//  ChooseBank.m
//  DaoZiJinFu
//
//  Created by woker on 16/6/24.
//  Copyright © 2016年 iwoker. All rights reserved.
//

#import "ChooseBank.h"
#import "BankListModel.h"

@interface ChooseBank () {
    NSMutableArray *listArray;
}

@property (retain, nonatomic) BankListModel *model;

@end

@implementation ChooseBank

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitleView:@"选择银行"];
    self.view.backgroundColor = BACKGROUND_COLOR;
    [self leftButton:nil];
    listArray = [NSMutableArray array];
    [self getBankInofrmation];
}

- (void)getBankInofrmation {
    _model = [[BankListModel alloc] init];
    JSVIEWSHOW;
    DZRequestUtil *request = [[DZRequestUtil alloc] init];
    request.requestUrl = @"Index/getBankList";
    request.httpMethod = @"GET";
    [DZNetAPI request:request success:^(id response) {
        NSLog(@"%@",response);
        JSVIEWHIDE;
        DZResponse *model = [DZResponse mj_objectWithKeyValues:response];
        if (model.rt == 1) {
            NSArray *array = model.data;
            for (NSDictionary *dic in array) {
                _model = [BankListModel modelWithDict:dic];
                [listArray addObject:_model];
            }
            [self.tableView reloadData];
        }else{
            JSSHOW(model.msg);
        }
    } failure:^(NSError *error) {
        JSVIEWHIDE;
        JSSHOW(LOAD_FAIL_TIP);
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CHOOSEBANK_CELL];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CHOOSEBANK_CELL];
    }
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    BankListModel *bankModel = listArray[indexPath.row];
    
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(12, 10, 50, 50)];
    [img sd_setImageWithURL:[NSURL URLWithString:bankModel.logo_path] placeholderImage:[UIImage imageNamed:LOGO_PLACEHOLDER]];
    [cell.contentView addSubview:img];
    UILabel *cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(74, 10, SCREEN_WIDTH-86, 20)];
    cellTitle.text = bankModel.name;
    cellTitle.textColor = DEFAULTCOLOR;
    [cell.contentView addSubview:cellTitle];
    UILabel *cellSubTitle = [[UILabel alloc] initWithFrame:CGRectMake(74, 40, SCREEN_WIDTH-86, 20)];
    if (bankModel.limit_desc) {
        cellSubTitle.text = bankModel.limit_desc;
    }
    cellSubTitle.textColor = [UIColor lightGrayColor];
    cellSubTitle.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:cellSubTitle];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 4;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BankListModel *bankModel = listArray[indexPath.row];
    NSDictionary *data = @{@"bankName":bankModel.name,
                           @"bankID":bankModel.id,
                           @"bankIcon":bankModel.logo_path,
                           @"pay_id":bankModel.pay_id
                           };
    [[NSNotificationCenter defaultCenter]postNotificationName:@"returnBankName" object:self userInfo:data];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
